#!/bin/sh

print_compiling_usage ()
{
    echo ""
    echo "    test_runner.sh compile <path-to-tests> <compiler>"
    echo ""
}

print_running_usage ()
{
    echo ""
    echo "    test_runner.sh run <path-to-tests>"
    echo ""
}

print_statistics_usage ()
{
    echo ""
    echo "    test_runner.sh stats <path-to-tests> <path-to-csv-summary>"
    echo ""
}

if [ "$#" -eq 0  ]; then
    echo "Usage:"
    echo "1) Batch compiling test cases"
    print_compiling_usage
    echo "2) Batch running test cases"
    print_running_usage
    echo "3) Collect statistics of test cases"
    print_statistics_usage
fi

if [ "$1" = "compile" ]; then
    cases=$(find $2 | grep \\.c$)
    length=$(echo "$cases" | wc -l)
    echo "[info] $length test cases will be compiled"

    i=0
    failed=0
    for file in $cases
    do
        ((i++))

        # cleaning existing stuff
        rm -f $file.out
        rm -f $file.err.compile

        $3 $file -o $file.out > $file.log.compile 2>&1
        if [ $? -eq 0 ]; then
            echo "($i / $length) succeeded compiling $file"
        else
            ((failed++))
            mv $file.log.compile $file.err.compile
            echo "($i / $length) failed compiling $file"
        fi
    done

    echo "=============================================================="
    echo "$length test cases in total, $failed of them failed to compile"
    if [ $failed -ne 0 ]; then
        echo ""
        echo "please run 'find $2 | grep .err.compile\$'" for details
    fi
    exit 0
fi

if [ "$1" = "run" ]; then
    cases=$(find $2 | grep \\.out$)
    length=$(echo "$cases" | wc -l)
    echo "[info] $length test cases will be compiled"

    currdir=$(pwd)
    tmpdir=$(mktemp -d)

    i=0
    failed=0
    for file in $cases
    do
        ((i++))
        mkdir -p $tmpdir
        cp $file $tmpdir/sut
        cd $tmpdir
        timeout 10s sh -c ./sut > ./sut.log.run 2>&1
        retval=$?
        cd $currdir

        if [ $retval -eq 0 ]; then
            echo "($i / $length) succeeded running $file"
        else
            ((failed++))
            mv $tmpdir/sut.log.run $file.err.run
            echo "($i / $length) failed running $file"
        fi
        cd $currdir
        rm -rf $tmpdir
    done
    echo "=============================================================="
    echo "$length test cases in total, $failed of them failed to run"
    if [ $failed -ne 0 ]; then
        echo ""
        echo "please run 'find $2 | grep .err.run\$'" for details
    fi
    exit 0
fi

if [ "$1" = "stats" ]; then
    cases=$(find $2 | grep \\.txt$)
    length=$(echo "$cases" | wc -l)
    echo "[info] $length test cases will be collected"

    if [ "$3" = "" ]; then
        echo "[error] target csv file not provided"
        exit 1
    fi

    touch $3
    echo "test scene,case generated,case compiled,case passed" > $3
    i=0
    for file in $cases
    do
        basename=${file%.txt}
        if [ ! -f $basename.c ]; then
            echo "$basename,no,no,no" >> $3
            continue
        fi
        if [ ! -f $basename.c.out ]; then
            echo "$basename,yes,no,no" >> $3
            continue
        fi
        if [ -f $basename.c.out.err.run ]; then
            echo "$basename,yes,yes,no" >> $3
            continue
        fi
        echo "$basename,yes,yes,yes" >> $3
    done
    exit 0
fi
